package com.suse.ecommerce.controller;

import com.suse.ecommerce.pojo.Address;
import com.suse.ecommerce.pojo.User;
import com.suse.ecommerce.service.AddressService;
import com.suse.ecommerce.uilts.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.List;

@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;
    @RequestMapping("/auth/addAddress")
    public Msg addAddress(@SessionAttribute("user") User user, Address address){
        address.setUserId(user.getUserId());
        addressService.addAddress(address);
        return Msg.success("插入地址成功");
    }
    @RequestMapping("/auth/queryAddress")
    public Msg addAddress(@SessionAttribute("user") User user){
        List<Address> addresses = addressService.queryAllAdress(user.getUserId());
        return Msg.success("插入地址成功").add("addresses",addresses);
    }
    @RequestMapping("/address/queryAddressById")
    public Msg queryAddressById(Address address){
        if(address.getAddressId() == null) {
            return  Msg.error("没有id");
        }else{
            System.out.println(address.getAddressId());
            int id = Integer.valueOf(address.getAddressId()+"");
            Address address1 = addressService.getAddressById(id);
            return Msg.success("成功").add("address",address1);
        }
    }
}
